<?php
namespace app\adminmall\controller;
use app\adminmall\model\LineMemberModel;
use app\web\model\MemberLevelMode;
use app\common\controller\AdminBaseController;
use think\Config;
use think\Db;

/**
 * 线下用户管理
 * Class Member
 * @package app\admin\controller
 */
class LineMemberController extends AdminBaseController {
    protected $model;

    protected function _initialize() {
        parent::_initialize();
        $this->model = new LineMemberModel();
		//会员等级
		$this->modelevel = new MemberLevelMode();
        $member_level = $this->modelevel->select();
		$this->assign('member_level',$member_level);
    }

    /**
     * 用户管理
     * @param int    $page
     * @return mixed
     */
    public function index($page = 1) {
    	$where = [];
    	if(request()->isPost()){
		 	$param = input('post.');
			//会员名称
			if($param['membername']){
				$where['membername'] = ['like', '%'.$param['membername'].'%'];
			}
			//手机号码
			if($param['mobile']){
				$where['mobile'] = ['like', '%'.$param['mobile'].'%'];
			}
			//会员卡号
			if($param['memberNo']){
				$where['memberNo'] = ['like', '%'.$param['memberNo'].'%'];
			}	
			//有效期
			if($param['start_time']&&$param['end_time']){
				$where['validity_date'] = ['between time', [$param['start_time'],$param['end_time']]];
			}	
			//商家名称
			if($param['store_name']){
				$where['store_name'] = ['like', '%'.$param['store_name'].'%'];
			}			 				
	 	}
	
	 	$list = $this->model->where($where)->order('reg_date desc')->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }
	
	/**
     * [export导出]
     * @author [longx] 
     */
    public function export(){
 		$map = [];
    	if(request()->isPost()){
		 	$param = input('post.');
			//用户名称
			if($param['realname']){
				$map['realname'] = ['like', '%'.$param['realname'].'%'];
			}
			//手机号码
			if($param['mobile']){
				$map['mobile'] = ['like', '%'.$param['mobile'].'%'];
			}
			//商家名称
			if($param['store_name']){
				$map['b.store_name'] = ['like', '%'.$param['store_name'].'%'];
			}	
			//时间搜索
			if($param['add_time'] && $param['end_time']){
				$map['a.time'] = ['between time', [$param['add_time'],$param['end_time']]];
			}		 				
	 	}
	
	 	$list = Db::name('mall_linemember')
	 	->alias('a')
	 	->join('mall_agent b','a.agent_id=b.id')
	 	->field('a.id,a.realname,a.mobile,b.store_name,a.score,a.time')
	 	->where($map)
		->paginate(10);
		
        $str = "姓名,手机号码,商家名称,积分,时间,\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
            $realname = iconv('utf-8','gbk',$row['realname']); //中文转码 
            $mobile = iconv('utf-8','gbk',$row['mobile']); 			
			$store_name = iconv('utf-8','gbk',$row['store_name']);			             
            $score = iconv('utf-8','gbk',$row['score']);
			$time = iconv('utf-8','gbk',$row['time']);                  
            $str .= $realname.",".$mobile.",".$store_name.",".$score.",".$time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='线下会员列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
     }
	
    /**
     * 增积分
     * @param int    $page
     * @return mixed
     */
    public function integral($id,$integral) {
    	    $a='';
       		$member = $this->model->where('id',$id)->find();	
       		$a = $member['total_score'] + $integral;
		if ($this->model->allowField(true)->save(['total_score'=>$a],['id'=>$id]) !== false) {
        	$this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
    /**
     * 减积分
     * @param int    $page
     * @return mixed
     */
    public function integral2($id,$integral) {
    	    $a='';
       		$member = $this->model->where('id',$id)->find();	
       		$a = $member['total_score']-$integral;
		if ($this->model->allowField(true)->save(['total_score'=>$a],['id'=>$id]) !== false) {
        	$this->success("操作成功");
        } else {
            $this->error('操作失败');
        }
    }
}

  